package org.example.demo1.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.demo1.config.MonitorConfig;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
@RequestMapping("/api/test")
@RequiredArgsConstructor
public class TestController {
    private final MonitorConfig monitorConfig;
    private final RestTemplate restTemplate;

    @GetMapping("/grafana")
    public ResponseEntity<String> testGrafana() {
        try {
            log.info("测试Grafana连接，URL: {}", monitorConfig.getGrafana().getUrl());
            
            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Bearer " + monitorConfig.getGrafana().getApiKey());
            
            String url = monitorConfig.getGrafana().getUrl() + "/api/health";
            log.info("请求URL: {}", url);
            
            ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                new HttpEntity<>(headers),
                String.class
            );
            
            log.info("Grafana响应状态码: {}", response.getStatusCode());
            return response;
        } catch (Exception e) {
            log.error("测试Grafana连接失败", e);
            return ResponseEntity.status(500).body("连接失败: " + e.getMessage());
        }
    }
} 